package com.company.cloud.user.client;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.company.cloud.common.asserts.ValidatorUtils;
import com.company.cloud.common.bean.BaseClientResult;
import com.company.cloud.common.bean.LoginSimpleUserInfo;
import com.company.cloud.user.modules.api.request.*;
import com.company.cloud.user.modules.api.response.AuthUrlConfigQueryResp;
import com.company.cloud.user.modules.api.response.AuthUrlConfigResp;
import com.company.cloud.user.modules.api.response.UserBaseResp;
import com.company.cloud.user.modules.api.service.UrlAuthPlatformRemoteService;
import com.company.cloud.user.modules.mgr.service.UrlAuthConfigMgrService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@Service
public class UrlAuthPlatformRemoteServiceImpl implements UrlAuthPlatformRemoteService {

    @Autowired
    private UrlAuthConfigMgrService urlAuthConfigMgrService;


    @Override
    public BaseClientResult<String> addAuthUrlConfigBatch(ServiceUrlConfigAddRequest params) {
        try {
            urlAuthConfigMgrService.addBatch(params);
            return BaseClientResult.success();
        }catch (Exception e){
            return BaseClientResult.error(e);
        }

    }

    @Override
    public BaseClientResult<String> initAuthUrlConfigCache() {
        try {
            urlAuthConfigMgrService.addUrl2Cache();
            return BaseClientResult.success();
        }catch (Exception e){
            return BaseClientResult.error(e);
        }
    }

    @Override
    public BaseClientResult<AuthUrlConfigQueryResp> queryAuthUrlConfig(AuthUrlConfigQueryRequest request) {
        try {
            IPage<AuthUrlConfigResp> page = urlAuthConfigMgrService.query(request);
            AuthUrlConfigQueryResp data=new AuthUrlConfigQueryResp();
            data.setRecords(page.getRecords());
            data.setTotal(page.getTotal());
            data.setPages(page.getPages());
            data.setSize(page.getSize());
            data.setCurrent(page.getCurrent());
            return BaseClientResult.success(data);
        } catch (Exception e) {
            return BaseClientResult.error(e);
        }
    }

    @Override
    public BaseClientResult<AuthUrlConfigResp> addAuthUrlConfig(AuthUrlConfigAddRequest request) {
        try {
            ValidatorUtils.validate(request);
            LoginSimpleUserInfo userInfo=new LoginSimpleUserInfo();
            userInfo.setId(request.getOpUserId());
            userInfo.setIp(request.getOpIp());
            AuthUrlConfigResp data=urlAuthConfigMgrService.add(request,userInfo);
            return BaseClientResult.success(data);
        } catch (Exception e) {
            return BaseClientResult.error(e);
        }
    }

    @Override
    public BaseClientResult<AuthUrlConfigResp> updateAuthUrlConfig(AuthUrlConfigUpdateRequest request) {
        try {
            ValidatorUtils.validate(request);
            LoginSimpleUserInfo userInfo=new LoginSimpleUserInfo();
            userInfo.setId(request.getOpUserId());
            userInfo.setIp(request.getOpIp());
            AuthUrlConfigResp data=urlAuthConfigMgrService.update(request,userInfo);
            return BaseClientResult.success(data);
        } catch (Exception e) {
            return BaseClientResult.error(e);
        }
    }

    @Override
    public BaseClientResult<AuthUrlConfigResp> getAuthUrlConfig(AuthUrlConfigGet2DelRequest request) {
        try {
            ValidatorUtils.validate(request);
            AuthUrlConfigResp data=urlAuthConfigMgrService.get(request.getId());
            return BaseClientResult.success(data);
        } catch (Exception e) {
            return BaseClientResult.error(e);
        }
    }

    @Override
    public BaseClientResult<String> delAuthUrlConfig(AuthUrlConfigGet2DelRequest request) {
        try {
            ValidatorUtils.validate(request);
            LoginSimpleUserInfo userInfo=new LoginSimpleUserInfo();
            userInfo.setId(request.getOpUserId());
            userInfo.setIp(request.getOpIp());
            urlAuthConfigMgrService.del(request.getId(),userInfo);
            return BaseClientResult.success();
        } catch (Exception e) {
            return BaseClientResult.error(e);
        }
    }


}
